﻿using Architecture.Api.Application.Command.MenuCommand;
using Architecture.Api.Dtos;
using Architecture.Domain.Dto;
using Architecture.Domain.Entity;
using Architecture.Domain.RBAC;
using Architecture.Repository.Impl;
using Architecture.Repository.Interface;
using Architecture.Service;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace Architecture.Api.Controllers
{
    /// <summary>
    /// 菜单Api控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class MenuController : ControllerBase
    {
        protected readonly IMenuService menuService;
        //中介者
        protected readonly IMediator mediator;
        /// <summary>
        /// 菜单构造函数
        /// </summary>
        /// <param name="menuService"></param>
        public MenuController(IMenuService menuService, IMediator mediator)
        {
            this.menuService = menuService;
            this.mediator = mediator;
        }
        /// <summary>
        /// 菜单添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddAsync(MenuCreateCommand model)
        {
            ApiResult<int> result= new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(model);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 父级菜单下拉框
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Menu>>> GetNameAsync()
        {
            ApiResult<List<Menu>> result= new ApiResult<List<Menu>>();
            try
            {
                result.Data= await mediator.Send(new MenuFrameCommand());
            }
            catch (Exception ex)
            {
                result.Code= -1;
                result.Message= ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 菜单列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageListDto<MenuDto>>> GetAllAsync([FromQuery]MenuShowCommand menu)
        {
            ApiResult<PageListDto<MenuDto>> result= new ApiResult<PageListDto<MenuDto>>();
            try
            {
                result.Data = await mediator.Send(menu);
            }
            catch (Exception ex)
            {
                result.Code= -1;
                result.Message= ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 菜单查看详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Menu>> GetByIdAsync([FromQuery]MenuBackfillCommand id)
        {
            ApiResult<Menu> result= new ApiResult<Menu>();
            try
            {
                result.Data = await mediator.Send(id);
            }
            catch (Exception ex)
            {
                result.Code=-1;
                result.Message= ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 菜单修改
        /// </summary>
        /// <param name="model">修改实体</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateAsync(MenuUpdateCommand model)
        {
            ApiResult<int> result= new ApiResult<int>();
            try
            {
                result.Data= await mediator.Send(model);
            }
            catch (Exception ex)
            {
                result.Code= -1;
                result.Message= ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 菜单删除
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResult<int>> Delete([FromQuery]MenuDeleteCommand model)
        {
            ApiResult<int> result= new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(model);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message= ex.Message;
                throw;
            }
            
            return result;
        }
        /// <summary>
        /// 菜单权限树形级联
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Cascader>>> MenuList(int id)
        {
            ApiResult<List<Cascader>> result= new ApiResult<List<Cascader>>();
            try
            {
                result.Data = await menuService.MenuList(id);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message= ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 菜单权限反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<object> PrivileFan(int id)
        {
            var list = await menuService.PrivileFan(id);
            return list;
        }
        /// <summary>
        /// 角色菜单关联添加
        /// </summary>
        /// <param name="rId"></param>
        /// <param name="mId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> GetRelatedAdd(PrivilegeAddCommand privileges)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(privileges);
            }
            catch (Exception ex)
            {
                result.Code= -1;
                result.Message= ex.Message;
                throw;
            }
            return result;
        }
    }
}
